package com.yjhb.android.sbz.activity;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.adapter.TimerAdapter;
import com.yjhb.android.sbz.entity.Timer;
import com.yjhb.android.sbz.manager.ResManager;
import com.yjhb.android.sbz.mvp.timerlist.TimerListPresenter;
import com.yjhb.android.sbz.mvp.timerlist.TimerListView;
import com.yjhb.android.sbz.tool.EventMsg;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.tool.ZxNetTool;
import com.jojo.android.zxlib.tool.ZxToast;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;
import com.yanzhenjie.recyclerview.widget.DefaultLoadMoreView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/10 14:56
 */
public class TimerListActivity extends BaseActivity<TimerListPresenter> implements BaseRecyclerAdapter.ItemClickListener<Timer>, OnItemMenuClickListener, TimerListView {
    private final String TAG = "TimerToppingListActivity";
    public final static int REQUEST_SET_TIMER = 658;


    @BindView(R.id.swipe_recyclerview)
    SwipeRecyclerView swipeMenuRecyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.title_tv)
    TextView title_tv;

    TimerAdapter timerAdapter;
    Integer taskId;
    Integer timerType;
    final int size = 25;
    int current = 1;
    ArrayList<Timer> timers = new ArrayList<>();

    @Override
    protected TimerListPresenter initPresenter() {
        return new TimerListPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tiemr_topping_list;
    }

    @Override
    protected void initUI() {
        taskId = getIntent().getIntExtra("taskId", -1);
        if (taskId == -1) {
            ZxToast.showINFOBottomToast("任务ID异常，请联系管理员。");
            finish();
        }
        timerType = getIntent().getIntExtra("timerType", -1);
        if (timerType == -1) {
            ZxToast.showINFOBottomToast("计时器类型异常，请联系管理员。");
            finish();
        }
        if (timerType == 1) {
            title_tv.setText("定时置顶");
        } else if (timerType == 2) {
            title_tv.setText("自动刷新");
        }
        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary),
                getResources().getColor(R.color.colorPrimaryDark),
                getResources().getColor(R.color.colorPrimaryOtherDark));
        swipeMenuRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        swipeMenuRecyclerView.setLongPressDragEnabled(false);
        swipeMenuRecyclerView.setItemViewSwipeEnabled(false);
        swipeMenuRecyclerView.setAutoLoadMore(true);
        swipeMenuRecyclerView.setLoadMoreView(new DefaultLoadMoreView(this));

        swipeMenuRecyclerView.setSwipeMenuCreator(new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int position) {
                SwipeMenuItem deleteItem = new SwipeMenuItem(getActivity())
                        .setBackgroundColorResource(R.color.red_00)
//                    .setImage(R.mipmap.ic_action_delete) // 图标。
                        .setText("删除") // 文字。
                        .setTextColor(Color.WHITE) // 文字颜色。
                        .setTextSize(16) // 文字大小。
                        .setWidth(ResManager.dp() * 80)
                        .setHeight(-1);
                rightMenu.addMenuItem(deleteItem);// 添加一个按钮到右侧侧菜单。.
            }
        });

    }

    @Override
    protected void initData() {
        loadingDialog.setTipsText("正在加载...");
        loadingDialog.show();
        swipeMenuRecyclerView.setAdapter(timerAdapter = new TimerAdapter(this));
        timerAdapter.setOnItemClick(this);
        timerAdapter.setData(timers);
        current = 1;
        presenter.getListTimer(taskId, timerType, size, current);
    }

    @Override
    protected void regEvent() {

        swipeMenuRecyclerView.setOnItemMenuClickListener(this);
        swipeMenuRecyclerView.setLoadMoreListener(new SwipeRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {
                current++;
                presenter.getListTimer(taskId, timerType, size, current);
            }
        });
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                current = 1;
                presenter.getListTimer(taskId, timerType, size, current);
            }
        });
    }


    @OnClick(R.id.addMainTop)
    void addMainTop() {
        if (timerType == 1) {
            Intent intent = new Intent(this, SettingTimerActivity.class);
            intent.putExtra("taskId", taskId);
            intent.putExtra("timerType", timerType);
            startActivityForResult(intent, REQUEST_SET_TIMER);
        } else if (timerType == 2) {
            Intent intent = new Intent(this, SettingAutoRefreshTimerActivity.class);
            intent.putExtra("taskId", taskId);
            intent.putExtra("timerType", timerType);
            intent.putExtra("timerList",timers);
            startActivityForResult(intent, REQUEST_SET_TIMER);
        }
    }

    @OnClick(R.id.back)
    void back() {
        finish();
    }

    @Override
    public void onItemClick(int index, Timer timer) {
        if (timerType == 2) {
            Intent intent = new Intent(this, SettingAutoRefreshTimerActivity.class);
            intent.putExtra("taskId", taskId);
            intent.putExtra("timer", timer);
            startActivityForResult(intent, REQUEST_SET_TIMER);
        }
    }

    @Override
    public void onViewClick(View view, String msg, int index, Timer timer) {
        if (msg.equals("switch")) {
            int state = timer.getState() == 0 ? 1 : 0;
            presenter.changeState(index,timer.getId(), state);
            timer.setState(state);
        }
    }

    @Override
    public void onItemClick(SwipeMenuBridge menuBridge, int adapterPosition) {
        menuBridge.closeMenu();
        presenter.changeState(adapterPosition,timers.get(adapterPosition).getId(), 2);
    }

    @Override
    public void onEventMsg(EventMsg msg) {
        super.onEventMsg(msg);
        if (msg.msg == EventMsg.REQUEST_REFRESH_TIMER_LIST) {
            current = 1;
            presenter.getListTimer(taskId, timerType, size, current);
        }
    }

    @Override
    public void onSuccess(int index,Integer data) {
        loadingDialog.cancel();
        if (data == 2){
            timers.remove(index);
            timerAdapter.notifyItemRemoved(index);
        }
    }

    @Override
    public void onWait(String tips) {
        loadingDialog.setTipsText(tips);
        loadingDialog.show();
    }

    @Override
    public void onLoad(List<Timer> timers, boolean isRefresh) {
        loadingDialog.cancel();
        if (isRefresh) {
            this.timers.clear();
            this.timers.addAll(timers);
            swipeRefreshLayout.setRefreshing(false);
        } else {
            this.timers.addAll(timers);
            swipeMenuRecyclerView.loadMoreFinish(timers.size() == 0, timers.size() == size);
        }
        timerAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        if (code == -2)
            swipeMenuRecyclerView.loadMoreError(code, msg);
        if (!ZxNetTool.isNetworkAvailable(this)) {
            ZxToast.showINFOBottomToast("网络连接异常~~");
        }
    }
}
